package com.cn.qgx428.orderingmeal.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.myClass.Car;
import com.cn.qgx428.orderingmeal.myClass.Food;
import com.cn.qgx428.orderingmeal.viewHolder.RViewHolder;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

/**
 * Created by QinX on 2017/5/11.
 */

public class CarAdapter extends RBaseAdapter {
    private static final String TAG = "CarAdapter";

    private Context context;

    private List<Double> sum; //记录每项食物总价格
    private List<Boolean> options; //记录食物是否被选取
    private Double total ; // 记录选中的食物总价格

    private static final int ADD_FOOD = 0;
    private static final int SUB_FOOD = 1;

    private OnItemCheckListener cListener;

    private OnItemAmountChangedListener aListener;

    public interface OnItemCheckListener{
        void onItemChecked (int position);
    }

    public interface OnItemAmountChangedListener{
        void onAmountChanged(int position);
    }

    public CarAdapter(Context context, List list, int itemLayoutId) {
        super(context, list, itemLayoutId);
        this.context =context;
        this.sum = new ArrayList<Double>();
        this.options = new ArrayList<Boolean>();
        for (int i = 0; i < list.size(); i++){
            sum.add(0.0);
            options.add(false);
        }
    }

    @Override
    public void convert(final RViewHolder holder, Object item) {
        LinearLayout delelteBtn = holder.getView(R.id.car_item_foodDeleteLayout);
        ImageView pic = holder.getView(R.id.car_item_foodImg);
        TextView  foodName = holder.getView(R.id.car_item_foodName);
        final TextView foodPrice = holder.getView(R.id.car_item_foodPrice);
        TextView addBtn  = holder.getView(R.id.car_item_food_add);
        TextView subBtn = holder.getView(R.id.car_item_food_sub);
        final TextView amount = holder.getView(R.id.car_item_food_amount);
        CheckBox foodCheck = holder.getView(R.id.car_item_food_check);

        Car car =(Car)item;
        Food food = car.getFood();
        Picasso.with(context).load(food.getPic().getFileUrl()).fit().into(pic);
        foodName.setText(food.getName());
        amount.setText(String.valueOf(car.getAmount()));
        //设置价格信息
        final double finalPrice = food.getPrice();
        int position = holder.getAdapterPosition();
        setFoodPrice(foodPrice , finalPrice, amount,position);
        //设定改变食物数量事件
        if (aListener != null){
            addBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String No = changeAmount(amount.getText().toString(),ADD_FOOD);
                    amount.setText(No);
                    int position = holder.getAdapterPosition();
                    setFoodPrice(foodPrice , finalPrice, amount,position);
                    changeFoodPriceTotal();
                    aListener.onAmountChanged(position);
                }
            });
            subBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String No = changeAmount(amount.getText().toString(),SUB_FOOD);
                    amount.setText(No);
                    int position = holder.getAdapterPosition();
                    setFoodPrice(foodPrice , finalPrice, amount,position);
                    changeFoodPriceTotal();
                    aListener.onAmountChanged(position);
                }
            });
        }

        //设置选定食物按钮接口
        if (cListener != null){
            foodCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int position = holder.getAdapterPosition();
                    changeFoodOptions(position , isChecked);
                    cListener.onItemChecked(position);
                }
            });
        }
    }

    /**
     * 改变选中的食物总价
     */
    private void changeFoodPriceTotal() {
        Double _total = 0.0;
        for (int i = 0; i < sum.size(); i++){
            if (options.get(i) == true){
                _total += sum.get(i);
            }
        }

        total = _total;
    }

    public String getTotalPrice(){
        return "￥"+String.valueOf(total);
    };

    public Double getTotalPriceOfDouble(){
        return total;
    }

    /**
     * 改变选中的食物数组
     * @param position
     * @param isChecked
     */
    private void changeFoodOptions(int position, boolean isChecked) {
        if (isChecked){
            options.set(position,true);
        } else {
            options.set(position,false);
        }
        changeFoodPriceTotal();
        printOptions();
    }

    public void printOptions(){
        for (int i =0 ;i < options.size(); i++){
            if (options.get(i) == false){
                Log.d(TAG, "printOptions: "+0);
            } else{
                Log.d(TAG, "printOptions: "+1);
            }
        }
    }

    private String changeAmount(String s, int way) {
        int No = Integer.parseInt(s);
        if (way == ADD_FOOD){
            No = No + 1;
        } else if(way == SUB_FOOD) {
            if (No == 1){
                ;
            }
            else{
                No = No-1;
            }
        }
        return String.valueOf(No);
    }

    private void setFoodPrice(TextView foodPrice, Double price, TextView amount,int position) {
        String str = "￥";
        int am = Integer.parseInt(amount.getText().toString());
        str = str+String.valueOf(price * am);
        Double total = am * price;
        sum.set(position,total);
        foodPrice.setText(str);
    }

    public List<Boolean> getOptions(){
        return options;
    }

    public List<Double> getItemTotalPrice(){
        return sum;
    }

    //获取是否无食物被选中
    public Boolean isHasFoodChecked(){
        Boolean has = false;
        for (int i=0; i < options.size(); i++){
            if (options.get(i)){
                has = true;
                break;
            }
        }
        return has;
    }

    public void setFoodCheckListener(OnItemCheckListener clistener){
        this.cListener = clistener;
    }

    public void setItemAmountChangedListener(OnItemAmountChangedListener aListener){
        this.aListener = aListener;
    }

    public void removeData(int position){
        sum.remove(position);
        options.remove(position);
    }

}
